/**
 * Copyright (C) 2011 Audit Logger Project. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.auditlogger.config.providers;

import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.InvalidKeySpecException;

import org.auditlogger.config.ConfigurationServices;
import org.auditlogger.config.spi.PropertiesConfigurator;
import org.auditlogger.config.util.ConfigurationException;
import org.auditlogger.config.util.Constants;
import org.auditlogger.util.Base64EncoderDecoder;

/**
 * Allows the configuration of Audit Logger from an external file 
 * (configuration.properties). 
 * <p>
 * Applications may want to extend this to fetch encryption master key, 
 * signing public and private keys, etc. and other dynamic configuration
 * from an external source such as key management system. 
 * 
 * @author <a href="mailto:ravi.sharda@gmail.com">Ravi Sharda</a>
 */
public class DefaultPropertiesConfigurator extends PropertiesConfigurator {

	public String getAppName() {
		return PropertiesReader.getValueForStaticProperty(Constants.APP_NAME);
	}

	@Override
	public String getMessageDigestAlgorithm() {
		return PropertiesReader.getValueForStaticProperty(Constants.MSG_DIGEST_ALGO);
	}

	@Override
	public String getEncryptionCipherTransformation() {
		return PropertiesReader.getValueForStaticProperty(Constants.ENCRYPT_CIPHER_TRANS);
	}

	@Override
	public int getEncryptionCipherKeyLength() {
		String keyLength = PropertiesReader.getValueForStaticProperty(Constants.ENCRYPT_KEY_LENGTH);
		return Integer.parseInt(keyLength);
	}
	
	@Override
	public byte[] getMasterKey() {
		String encodedMasterKey = 
			PropertiesReader.getValueForStaticProperty(Constants.MASTER_KEY);
		try {
		    return Base64EncoderDecoder.decodeFromBase64(encodedMasterKey);
		} catch (IOException e) {
			throw new ConfigurationException(e.getMessage(), e);
		}
	}

	@Override
	public byte[] getMasterSalt() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public PublicKey getSignerPublicKey() {
		//String publicKey = 
			//PropertiesReader.getValueForStaticProperty(Constants.SIGNING_PUBLIC_KEY);
		try {
			// byte[] encodedKey = EncoderDecoder.decodeFromBase64(publicKey);
			//java.security.spec.EncodedKeySpec keySpec =
			
			BigInteger y= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.y"));
			BigInteger p= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.p"));
			BigInteger q= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.q"));
			BigInteger g= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.g"));
			
			DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, p, q, g);
				
			//X509EncodedKeySpec keySpec = 
				//new X509EncodedKeySpec(encodedKey);
			KeyFactory keyFactory = KeyFactory.getInstance(
					ConfigurationServices.getPropertiesConfigurator().getSignatureAlgorithm());
			return keyFactory.generatePublic(keySpec);
		} catch (NoSuchAlgorithmException e) {
			throw new ConfigurationException(e.getMessage(), e);
		} catch (InvalidKeySpecException e) {
			throw new ConfigurationException(e.getMessage(), e);
		} 
	}
	
	public PrivateKey getSignerPrivateKey() throws ConfigurationException {
		//String privateKey = 
			//PropertiesReader.getValueForStaticProperty(Constants.SIGNING_PRIVATE_KEY);
		try {
			//byte[] encodedKey = EncoderDecoder.decodeFromBase64(privateKey);
			//java.security.spec.EncodedKeySpec keySpec =
			
			BigInteger x= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.x"));
			BigInteger p= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.p"));
			BigInteger q= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.q"));
			BigInteger g= new BigInteger(PropertiesReader.getValueForStaticProperty("Signature.g"));
			
			DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(
					x, p, q, g);
			//X509EncodedKeySpec keySpec = 
				//new X509EncodedKeySpec(encodedKey);
			KeyFactory keyFactory = KeyFactory.getInstance(
					ConfigurationServices.getPropertiesConfigurator().getSignatureAlgorithm());
			return keyFactory.generatePrivate(keySpec);
		} catch (NoSuchAlgorithmException e) {
			throw new ConfigurationException(e.getMessage(), e);
		} catch (InvalidKeySpecException e) {
			throw new ConfigurationException(e.getMessage(), e);
		} 
	}
	
}
